<?php

class Admin_GroupsController extends Admin_Controller_Base {

   public function indexAction() {
      // this is the list action
      // get the data from the database and use the paginator
      $groupsModel = new Model_Group();
      $groups = $groupsModel->fetchAll();
      $this->view->groups = $groups;
      $this->_setTitle('Groups - List');
      $this->_setPaginator('groups');
   }

   public function addAction() {
      $this->_setTitle('Groups - Add');
      $form = new Admin_Form_Groups(array('params' => $this->getRequest()->getParams()));
      $this->view->form = $form;
      
      if($this->getRequest()->isPost()) {
         if($form->isValid($this->_request->getPost())) {
            $group = new Model_Group();
            //$group->id = $form->id->getValue();
            $group->name = $form->name->getValue(); 
            $group->insert();
            $this->_redirect("/admin/groups");
         }

      }
   }
   
   public function editAction() {
      $this->_setTitle('Groups - Edit');
      $form = new Admin_Form_Groups(array('params' => $this->getRequest()->getParams()));
      $this->view->form = $form;
      
      if($this->getRequest()->isPost()) {
         if($form->isValid($this->_request->getPost())) {
            $group = new Model_Group();
            $group->id = $form->id->getValue();
            $group->name = $form->name->getValue(); 
            $group->update();
            $this->_redirect("/admin/groups");
         }

      }
   }
   
   public function deleteAction() {
      $this->_setTitle('Groups - Delete');
      $params = $this->getRequest()->getParams();
      $group = new Model_Group();
      $group->id = $params['id'];
      $group->delete();
      $this->_redirect("/admin/groups");
   }

}
